<template>
	<view class="dialog">
		<el-dialog :title="title" :center="title != '新增二维码'" :visible.sync="dialogVisible" width="40%" :before-close="handleDialogClose">
			<!-- 合同审核 -->
			<el-form ref="form" label-width="130px">
				<el-form-item label="合同文件:" class="item">
					<view>
						<el-button type="primary" size="mini" @click="uploadClick" v-preventReClick>点击上传</el-button>
					</view>
					<view>{{ tip }}</view>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="handleClose(true)" v-preventReClick>{{ getBtnTitle(title) }}</el-button>
				<el-button @click="handleClose(false)">取 消</el-button>
			</span>
		</el-dialog>
	</view>
</template>
<script>
	import {
		Request
	} from '../../model/request.js'
	import {
		Util
	} from '../../utils/util.js'
	// import {
	// 	config
	// } from "../../api/config.js";
	// import {
	// 	ServerUrl
	// } from "../../api/server_url.js";
	export default {
		props: {
			dialogVisible: '',
			second: '',
			title: '',
			item: {}
		},
		
		data() {
			return {
				file: '',
				tip: '请点击上传文件'
			}
		},
		
		methods: {
			uploadClick() {
				uni.chooseFile({
					count: 1, 
				    success: (res) => {
						this.file = res.tempFiles[0]
						this.tip = res.tempFiles[0].name
				    }
				})
			},
			
			handlePreview(file) {
				console.log(file);
			},
			
			handleExceed(files, fileList) {
				this.$message.warning(`只能上传一个文件，如需覆盖，请删除之前的文件！`);
			},
			
			handleSuccess(res, file, fileList) {
				this.fileList = fileList
				console.log(res, file, fileList)
			},
			
			beforeRemove(file, fileList) {
				return this.$confirm(`确定移除 ${ file.name }？`);
			},
			
			getBtnTitle(t) {
				return '通过'
			},
			
			handleDialogClose(flag=false) {
				this.file = ''
				this.tip = ''
				this.$emit('closeDialog', flag)
			},
			
			async handleClose(e) {
				if (e == true) {
					if (this.file) {
						let formData = new FormData()
						formData.append("file", this.file)
						let resStr = await Request.FILEUPLOAD(this.file.path, formData)
						let result = JSON.parse(resStr)
						if (result.code == 200) {
							let res2 = await Request.CONTRACTUPDATE(this.item.id, result.data)
							if (res2.code == 200) {
								Util.showToast('操作成功')
								this.handleDialogClose(true)
							} else {
								this.handleDialogClose()
							}
						} else {
							this.handleDialogClose()
						}
					} else {
						return false
					}
				} else {
					this.handleDialogClose()
				}
			},
			
			loadCode(imgsrc, name) {
			  let image = new Image();
			  // 解决跨域 Canvas 污染问题
			  image.setAttribute("crossOrigin", "anonymous");
			  image.onload = function () {
				let canvas = document.createElement("canvas");
				canvas.width = image.width;
				canvas.height = image.height;
				let context = canvas.getContext("2d");
				context.drawImage(image, 0, 0, image.width, image.height);
				let url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
				let a = document.createElement("a"); // 生成一个a元素
				let event = new MouseEvent("click"); // 创建一个单击事件
				a.download = name || "code"; // 设置图片名称
				a.href = url; // 将生成的URL设置为a.href属性
				a.dispatchEvent(event); // 触发a的单击事件
			  };
			  image.src = imgsrc;
			}
		}
	}
</script>
<style scoped>
	.dialog {
		/* width: 100%;
	height: 100%; */
		position: absolute;
		left: 0;
		top: 0;
	}

	.el-form-item {
		position: relative;
		margin-bottom: 0;
	}

	.el-form-item::before {
		/* content: '*'; */
		position: absolute;
		left: 100px;
		top: 13px;
		color: red;
	}

	/deep/ .item .el-form-item__label {
		position: relative;
		left: -20px;
		/* color: red;
		border: solid 1px red;
		margin-left: 30px; */
	}

	/deep/ .item .el-form-item__label::before {
		content: ' ' !important;
	}

	.photo {
		width: 299px;
		height: 200px;
	}
	
	.code-content {
		text-align: center;
	}
</style>
